package com.example.demo.service.Impl;

import com.example.demo.dao.CheckDetailMapper;
import com.example.demo.dao.LocationStockMapper;
import com.example.demo.entity.Check.CheckDetailDTO;
import com.example.demo.entity.Check.LocationStock;
import com.example.demo.entity.Product.Files;
import com.example.demo.repository.FilesRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CheckDetailImpl implements com.example.demo.service.CheckDetailService {

    @Autowired
    private CheckDetailMapper checkDetailMapper;
    @Autowired
    private FilesRepository filesRepository;
    @Autowired
    private LocationStockMapper locationStockMapper;

    @Override
    public List<CheckDetailDTO> findByBillNo(String billNo) {
        //获取明细
        List<CheckDetailDTO> checkDetailDTOs = checkDetailMapper.findByBillNo(billNo);

        for (CheckDetailDTO checkDetailDTO : checkDetailDTOs) {
            String goodsCode = checkDetailDTO.getGoodsCode();
            //获取缩略图
            Files files = filesRepository.findImg(goodsCode);
            if (files != null) {
                checkDetailDTO.setImageUrl(files.getFilePathname());
            }
            //获取PDF文件
            files = filesRepository.findPDF(goodsCode);
            if (files != null) {
                checkDetailDTO.setPdfUrl(files.getFilePathname());
            }
            //获取库位
            List<LocationStock> locationStocks = locationStockMapper.findByGoodsCode(goodsCode);
            if (locationStocks != null && locationStocks.size() > 0) {
                String waLocationCode = "";
                for (LocationStock locationStock : locationStocks) {
                    waLocationCode = locationStock.getLocationCode()+" ";
                }
                checkDetailDTO.setWaLocationCode(waLocationCode);
            }
        }

        return checkDetailDTOs;
    }

    @Override
    public List<CheckDetailDTO> findByGoodsCode(Map<String,String> param) {//根据goods_code 获取盘点单明细
        List<CheckDetailDTO> checkDetailDTOS = checkDetailMapper.findByGoodsCode(param);
        System.out.println("checkDetailDTOS = " + checkDetailDTOS);
        if (checkDetailDTOS != null && checkDetailDTOS.size() > 0) {
            for (CheckDetailDTO checkDetailDTO : checkDetailDTOS) {
                String code = checkDetailDTO.getGoodsCode();
                //获取库位
                List<LocationStock> locationStocks = locationStockMapper.findByGoodsCode(code);
                if (locationStocks != null && locationStocks.size() > 0) {
                    String waLocationCode = "";
                    for (LocationStock locationStock : locationStocks) {
                        waLocationCode = locationStock.getLocationCode()+" ";
                    }
                    checkDetailDTO.setWaLocationCode(waLocationCode);
                }
            }
        }
        return checkDetailDTOS;
    }
}
